//
//  LoginViewController.swift
//  OnemeetingDemo
//
//  Created by 张衡 on 2020/4/17.
//  Copyright © 2020 www.onemeeting.1meetingIos. All rights reserved.
//

import Foundation

class LoginViewController: NSViewController {
    override func loadView() {
        let view = NSView.init(frame: NSMakeRect(0, 0, kHomeWinWidth, kHomeWinHeight))
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.white.cgColor
        self.view = view
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupUI()
    }
    override func viewWillAppear() {
        super.viewWillAppear()
    }
    
    
    lazy var backBtn: ZHButton = {
        let btn = ZHButton()
        btn.normalImage = NSImage.imageNamed("ic_close")!
        btn.target  = self
        btn.action = #selector(backBtnAction(senber:))
        return btn
    }()
   
    
    lazy var container: NSView = {
        let view = NSView()
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.white.cgColor
        return view
    }()
    
    func addChildrens(vcs:[NSViewController],selectIndex:Int?)  {
        for (inx,val) in vcs.enumerated() {
            self.addChild(val)
            if inx == selectIndex {
                self.container.addSubview(val.view)
                val.view.snp.makeConstraints { (make) in
                    make.edges.equalToSuperview().inset(NSEdgeInsetsMake(0, 0, 0, 0))
                }
            }
        }
    }
    
    func setupUI() {
        
        self.view.addSubview(container)
        container.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        self.addChildrens(vcs: [
            LoginAndViewController(),
            LoginWechatViewController()], selectIndex: 0)
        
        
        
        self.view.addSubview(backBtn)
        backBtn.snp.makeConstraints { (make) in
            make.size.equalTo(backBtn.image!.size)
            make.top.equalToSuperview().offset(20)
            make.left.equalToSuperview().offset(20)
        }
    }
    
    func switchTo(vc:NSViewController) {
        for sView in self.container.subviews {
            sView.removeFromSuperview()
        }
       
        self.container.addSubview(vc.view)
        vc.view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(NSEdgeInsetsMake(0, 0, 0, 0))
        }
    }


    @objc func backBtnAction(senber:ZHButton) {
        self.view.window?.close()
    }
}
